package com.flute.haflute.master;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.flute.haflute.common.ClusterConstants.COMMAND;
import com.flute.haflute.tools.ClusterUtils;

/**
 * 作业主节点（JM或者称为作业控制节点）侦听Agent代理命令和消息的接口
 * 
 */
public class MasterCommandHandler extends Thread {

	private static Logger logger = LoggerFactory
			.getLogger(MasterCommandHandler.class);

	public MasterCommandHandler(String name) {
		super(name);
	}

	public void run() {
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				System.in));
		String line;
		try {
			while ((line = reader.readLine()) != null) {
				// 收到集群关闭消息
				if (line.startsWith(COMMAND.SHUTDOWN)) {
					logger.warn("Received exit command, Master will exit");
					ClusterUtils.exitGracefully();
				}
			}
		} catch (IOException e) {
			logger.error("", e);
		}
	}
}
